/*获取容器宽高*/
var screenH = document.body.offsetHeight;
var screenW = document.body.offsetWidth;
//var screenH =$(".red-war").outerHeight();
//var screenW =$(".red-war").width();
//console.log(screenH)
//console.log(screenW)
var RX = screenW - $('.pk').width();
var RY = screenH - $('.pk').height();
var TIMER = '';
var TIMERCLOCK = '';

/*红包模块*/
function PACKAGE() {
    this.speed = '';
    this.x = '';
    this.y = '';
    this.luck_key = 999;
}

PACKAGE.prototype.init = function () { // 初始化
    this.basic();
    this.bindDom();
}

PACKAGE.prototype.randomNum = function(begin, end) { // 范围随机数生成器
    return Math.floor(Math.random()*(end - begin)) + begin;
}

PACKAGE.prototype.basic = function() {
    this.x = parseInt(this.randomNum(0, RX)); // 红包起始x坐标
    this.y = parseInt(this.randomNum(50, 300)); // 红包起始y坐标
    this.speed = this.randomNum(3000,5000); // 红包运动总时长
}

PACKAGE.prototype.eases = function() { // 红包运动模式
    var easeNum = parseInt(this.randomNum(0, 5));
    switch (easeNum) {
        case 0:
            return 'easeInQuad';
            break;
        case 1:
            return 'swing';
            break;
        case 2:
            return 'easeInExpo';
            break;
        case 3:
            return 'easeInQuint';
            break;
        case 4:
//		    easeInOutBounce
            return 'easeInOutQuad';
            break;
    }
}

PACKAGE.prototype.theme = function(num) { // 红包皮肤
    var themeNum = num || parseInt(this.randomNum(0, 3));
    switch (themeNum) {
        case 0:
            return 'pk1';
            break;
        case 1:
            return 'pk2';
            break;
        case 2:
            return 'pk3';
            break;
        case 3:
            return 'pk4';
            break;
    }
}

PACKAGE.prototype.bindDom = function() {
    var lock = this.randomNum(0, 20);
    var _key = this.luck_key;
    var luck = this.theme();
	/*参数*/
    if (_key == lock) {
        console.log('%cthis is package','background:#aaa;color:red;font-size:16px;');
        luck = this.theme(3);
    }
    console.log('key', _key);
    console.log('lock', lock);
    console.log('speed',this.speed);

    var pkDom = $('<div class="pk '+ luck +'" data-rom='+ lock +'></div>');
    pkDom.css({'left': this.x + $(window).width()/1.4  + 'px' ,'top': '-'+ this.y +'px',"z-index":"1"});
    pkDom.animate({ top : RY }, this.speed, this.eases(), function() {
        $(this).remove();
    });
    pkDom.on('touchstart', function() {
        var random = Math.random()*10;
        if ($(this).attr('data-rom') == _key) {
			/* 中奖操作 */
            console.log(random)
            if(random > 2 && random < 7){
                $(".red-alert").show();
                $(".get-red").show();
                $(".red-close").show();
                $(".count-down").hide();
                $(".get-redimg").attr("src","img/get-red.png");
            }else{
                $(".red-alert").show();
                $(".get-red").show();
                $(".red-close").show();
                $(".count-down").hide();
                $(".get-redimg").attr("src","img/get-red1.png");
            }

            clearInterval(TIMER);
            clearInterval(TIMERCLOCK);
            $(".red-close").on("click",function(){
                $(".red-alert").hide();
            });
			/*抢到红包  红包雨结束*/
//			$('.pk').remove();
        }

        $(this).addClass('boom');
        setTimeout(function() {
            $(this).remove();
        }.bind(this),200);

    })
    $('body').append(pkDom);
}

PACKAGE.prototype.start = function(alltime, amount) {
    var time = 0;
    alltime? alltime = alltime : alltime = 30000;
    amount ? amount =  amount : amount = 50;
    console.log(alltime);
    console.log(amount)
    var intervalTime = parseInt(alltime / amount);
    TIMER = setInterval(function() {
        pks.init();
        time++;
        if (time >= amount/2) { // 前半数红包不进行"钥匙"生成
            this.luck_key = this.randomNum(0, 20);
        }
        if (time > amount) {
            pks.stop();
        }
    }.bind(this), intervalTime);
}

PACKAGE.prototype.stop = function() {
    clearInterval(TIMER);
    clearInterval(TIMERCLOCK);
    $('.pk').remove();

    setTimeout(function(){
        $(".count-down").hide();
        $(".red-alert").show();
        $(".no-red").show();
        $(".red-close").show();
    },2000);

    var rand = Math.random()*10;
    if(rand < 5){
        $(".no-redimg").attr("src","img/no-red.png");
    }else{
        $(".no-redimg").attr("src","img/no-red1.png");
    }

    $(".red-close").on("click",function(){
        $(".red-alert").hide();
    });

}

var $clock = $('.clock');
function clock(time) {
    var times = time / 1000;
    $clock.html(times)
    TIMERCLOCK = setInterval(function() {
        times--;
        $clock.html(times);
        if (times <= 0) {
            pks.stop();
        }
    }, 1000);
}

var pks = new PACKAGE();


/*$().ready(function () {
    setInterval(function () {
        $.ajax({
            async: false,
            url: "/wxcs/redData",
            type: "post",
            data: {
                // userId: $('#userid').val()
            },
            success(data){
                console.log(data.da)
                if(data.da==1){
                    var wait = 5;
                    time();
                    function time(){
                        if(wait == 0){
                            $(".red-alert").hide();
//		var ok = confirm('请否开始游戏');
//		if (ok) {
                            clock(100000);
                            pks.start(30000, 150);
//		}
                        }else{
                            wait--;
                            if(wait == 4){
                                $(".red-alert").show();
                                $(".red-close").hide();
                                $(".count-down img").attr("src","http://s10.realdtech.com/xws/house-richer/richer-home/img/red-word.png");
                            }else if(wait == 3){
                                $(".red-alert").show();
                                $(".red-close").hide();
                                $(".count-down").css({"width":"24%","margin-left":"-12%"});
                                $(".count-down img").attr("src","http://s10.realdtech.com/xws/house-richer/richer-home/img/three.png");
                            }else if(wait == 2){
                                $(".red-alert").show();
                                $(".red-close").hide();
                                $(".count-down").css({"width":"24%","margin-left":"-12%"});
                                $(".count-down img").attr("src","http://s10.realdtech.com/xws/house-richer/richer-home/img/two.png");
                            }else if(wait == 1){
                                $(".red-alert").show();
                                $(".red-close").hide();
                                $(".count-down").css({"width":"20%","margin-left":"-10%"});
                                $(".count-down img").attr("src","http://s10.realdtech.com/xws/house-richer/richer-home/img/one.png");
                            }
                            setTimeout(function(){
                                time();
                            },1000);
                        }
                    }

                }else {

                }


            },
            error: function (data) {

            }
        })
    },9000)

})*/

// 游戏生成器
//$(document).ready(function() {

//	/!*模拟开始游戏*!/
// setInterval(function() {
// 	var myDate = new Date();
////	console.log(myDate)
////	console.log(myDate.getHours()+":"+myDate.getMinutes()+":"+myDate.getSeconds());
//	var date = myDate.getHours()+":"+myDate.getMinutes()+":"+myDate.getSeconds();
//	console.log(date)
//	if(date == "0:59:55" || date == "1:59:55" ||date == "2:59:55" ||date == "3:59:55"||date == "4:59:55"||date == "5:59:55"||date == "6:59:55"||date == "7:59:55" ||
//	date == "8:59:55" ||date == "9:59:55" ||date == "10:59:55" ||date == "11:59:55" ||date == "12:59:55" ||date == "13:59:55" ||date == "14:59:55"||date == "15:59:55"||
//	date == "16:59:55"||date == "17:59:55"||date == "18:59:55"||date == "19:59:55"||date == "20:59:55"||date == "21:59:55"||date == "22:59:55"||date == "23:59:55"){
//		console.log("高喆")
//	}
// },1000);
//     var wait = 5;
//     time();
//     function time(){
//         if(wait == 0){
//             $(".red-alert").hide();
// //		var ok = confirm('请否开始游戏');
// //		if (ok) {
//             clock(100000);
//             pks.start(30000, 150);
// //		}
//         }else{
//             wait--;
//             if(wait == 4){
//                 $(".red-alert").show();
//                 $(".red-close").hide();
//                 $(".count-down img").attr("src","img/red-word.png");
//             }else if(wait == 3){
//                 $(".red-alert").show();
//                 $(".red-close").hide();
//                 $(".count-down").css({"width":"24%","margin-left":"-12%"});
//                 $(".count-down img").attr("src","img/three.png");
//             }else if(wait == 2){
//                 $(".red-alert").show();
//                 $(".red-close").hide();
//                 $(".count-down").css({"width":"24%","margin-left":"-12%"});
//                 $(".count-down img").attr("src","img/two.png");
//             }else if(wait == 1){
//                 $(".red-alert").show();
//                 $(".red-close").hide();
//                 $(".count-down").css({"width":"20%","margin-left":"-10%"});
//                 $(".count-down img").attr("src","img/one.png");
//             }
//             setTimeout(function(){
//                 time();
//             },1000);
//         }
//     }
//
// })
